package com.vehicle.coupon.controller;

import java.io.BufferedReader;
import java.io.PrintWriter;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ch.qos.logback.core.joran.spi.XMLUtil;
import com.alibaba.fastjson.JSON;
import com.vehicle.common.core.domain.R;
import com.vehicle.common.core.utils.SecurityUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;
import com.vehicle.common.log.annotation.Log;
import com.vehicle.common.log.enums.BusinessType;
import com.vehicle.common.security.annotation.PreAuthorize;
import com.vehicle.coupon.domain.Coupon;
import com.vehicle.coupon.service.ICouponService;
import com.vehicle.common.core.web.controller.BaseController;
import com.vehicle.common.core.web.domain.AjaxResult;
import com.vehicle.common.core.utils.poi.ExcelUtil;
import com.vehicle.common.core.web.page.TableDataInfo;

/**
 * 优惠券Controller
 *
 * @author XU
 * @date 2025-03-25
 */
@RestController
@RequestMapping("/conpon")
public class CouponController extends BaseController {
    @Autowired
    private ICouponService couponService;

    /**
     * 查询优惠券列表
     */
    @PreAuthorize(hasPermi = "coupon:conpon:list")
    @GetMapping("/list")
    public TableDataInfo list(Coupon coupon)
    {
        startPage();
        List<Coupon> list = couponService.selectCouponList(coupon);
        return getDataTable(list);
    }

    /**
     * 导出优惠券列表
     */
    @PreAuthorize(hasPermi = "coupon:conpon:export")
    @Log(title = "优惠券", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Coupon coupon) throws IOException
    {
        List<Coupon> list = couponService.selectCouponList(coupon);
        ExcelUtil<Coupon> util = new ExcelUtil<Coupon>(Coupon.class);
        util.exportExcel(response, list, "优惠券数据");
    }

    /**
     * 获取优惠券详细信息
     */
    @PreAuthorize(hasPermi = "coupon:conpon:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(couponService.selectCouponById(id));
    }

    /**
     * 新增优惠券
     */
    @PreAuthorize(hasPermi = "coupon:conpon:add")
    @Log(title = "优惠券", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Coupon coupon)
    {
        return toAjax(couponService.insertCoupon(coupon));
    }

    /**
     * 修改优惠券
     */
    @PreAuthorize(hasPermi = "coupon:conpon:edit")
    @Log(title = "优惠券", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Coupon coupon)
    {
        return toAjax(couponService.updateCoupon(coupon));
    }

    /**
     * 删除优惠券
     */
    @PreAuthorize(hasPermi = "coupon:conpon:remove")
    @Log(title = "优惠券", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(couponService.deleteCouponByIds(ids));
    }

    /**
     * 发放优惠券
     */
    @PreAuthorize(hasPermi = "coupon:conpon:issue")
    @PostMapping("/issue")
    public AjaxResult issueCoupon(@RequestBody Map<String, Long> params) {
        Long couponId = params.get("couponId");
        Long userId = SecurityUtils.getUserId();
        // 如果targetUserId为空，则使用当前用户ID
        Long targetUserId = params.get("targetUserId");
        if (targetUserId == null) {
            targetUserId = userId;
        }
        
        try {
            boolean result = couponService.issueCoupon(couponId, userId, targetUserId);
            return result ? AjaxResult.success("发放成功") : AjaxResult.error("发放失败");
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }




}
